package com.erlin.common.security.entity;

import com.erlin.common.entity.ElPermission;
import com.erlin.common.entity.RolePermissionMid;
import com.erlin.platform.entity.ElUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Data
public class SysUserDetail implements UserDetails {
    private ElUser elUser;
    private List<ElPermission> permissionList;

    public SysUserDetail(ElUser elUser, List<ElPermission> permissionList) {
        this.elUser = elUser;
        this.permissionList = permissionList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return permissionList.stream()
                .filter(Objects::nonNull)
                .map(permission -> new SimpleGrantedAuthority(permission.getPermission()))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return elUser.getPassword();
    }

    @Override
    public String getUsername() {
        return elUser.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return Objects.equals(0,elUser.getStatus());
    }
}
